/**
 * Created by PhpStorm.
 * User: Devmc
 * Date: 2021/4/25
 * Time: 11:24
 */

import axios from "axios";
import store from '../store';
import utilWidget from '../utils/widget'


/**
 * 获取列表
 *
 * @param body
 * @param page
 * @param size
 * @return {Promise<unknown>}
 */
function getList(body = {}, page = 1, size = 500) {
    const url = store.state.api + `/announcement/list?pageNum=${page}&pageSize=${size}`;
    return new Promise((resolve) => {
        axios.post(url, body).then((response) => {
            if (response.data.code == 200) {
                resolve(response.data.data);
            }
        }).catch((error) => {
            console.log(error);
        });
    });
}


/**
 * 删除
 *
 * @param id
 * @return {Promise<unknown>}
 */
function del(id) {
    const url = store.state.api + '/announcement/del?ids=' + id;

    return new Promise((resolve) => {
        axios.delete(url)
            .then((response) => {
                utilWidget.showMessage(response.data.code == 200 && response.data.data);
                resolve(response.data);
            })
            .catch((error) => {
                console.log(error);
            });
    });
}


/**
 * 根据ID获取
 *
 * @param id
 * @return {Promise<unknown>}
 */
function getById(id) {
    const url = store.state.api + '/announcement/getById?announcementid=' + id;

    return new Promise((resolve) => {
        axios.get(url)
            .then((response) => {
                if (response.data.code == 200) {
                    resolve(response.data.data);
                }
            })
            .catch((error) => {
                console.log(error);
            });
    });
}

export default {
    getList,
    del,
    getById
}